package com.niiwoo.civet.account.service.local.refund.newRefund;

import com.alibaba.fastjson.JSON;
import com.niiwoo.asset.constant.MqConstants;
import com.niiwoo.asset.dto.mq.RepaymentResultDTO;
import com.niiwoo.civet.account.constant.AccountRabbitConstant;
import com.niiwoo.civet.account.dao.entity.BatchTransOrder;
import com.niiwoo.civet.account.dto.common.AccountBatchTransOrderDTO;
import com.niiwoo.civet.account.dto.request.RepaymentWithholdFailMsgMQDTO;
import com.niiwoo.civet.account.enums.RepayMsgTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by dell on 2018/1/31.
 * 消息通知服务
 */
@Service
@Slf4j
public class RefundMsgLocalService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void repaymentMessageSend(AccountBatchTransOrderDTO accountBatchTransOrderDTO, RepayMsgTypeEnum repayMsgTypeEnum){
        try {
            RepaymentWithholdFailMsgMQDTO repaymentWithholdFailMsgMQDTO = new RepaymentWithholdFailMsgMQDTO();
            repaymentWithholdFailMsgMQDTO.setUserId(accountBatchTransOrderDTO.getBorrowerUserId());
            repaymentWithholdFailMsgMQDTO.setProjectId(accountBatchTransOrderDTO.getProjectId());
            repaymentWithholdFailMsgMQDTO.setProjectTitle(accountBatchTransOrderDTO.getProjectTitle());
            repaymentWithholdFailMsgMQDTO.setRepayMsgTypeEnum(repayMsgTypeEnum);
            repaymentWithholdFailMsgMQDTO.setAmount(accountBatchTransOrderDTO.getAmount());
            repaymentWithholdFailMsgMQDTO.setCurrentPeriod(accountBatchTransOrderDTO.getCurrentPeriod());
            repaymentWithholdFailMsgMQDTO.setTotalPeriod(accountBatchTransOrderDTO.getTotalPeriod());
            repaymentWithholdFailMsgMQDTO.setRefundDate(accountBatchTransOrderDTO.getRefundDate());
            log.info("repaymentMessageSend={}", JSON.toJSONString(repaymentWithholdFailMsgMQDTO));
            rabbitTemplate.convertAndSend(AccountRabbitConstant.Exchange.REPAYMENT_MESSAGE_SEND, AccountRabbitConstant.RoutingKey.REPAYMENT_MESSAGE_SEND, repaymentWithholdFailMsgMQDTO);
        } catch (AmqpException e) {
            log.error("repaymentMessageSend",e);
        }
    }

    public void accountWithholdFailMsgSend(AccountBatchTransOrderDTO accountBatchTransOrderDTO){
        this.repaymentMessageSend(accountBatchTransOrderDTO,RepayMsgTypeEnum.ACCOUNT_WITHHOLD_FAIL);
    }

    public void cardWithholdFailMsgSend(AccountBatchTransOrderDTO accountBatchTransOrderDTO){
        this.repaymentMessageSend(accountBatchTransOrderDTO,RepayMsgTypeEnum.CARD_WITHHOLD_FAIL);
    }

    public void cardWithholdFailMsgSend(BatchTransOrder batchTransOrder){
        AccountBatchTransOrderDTO accountBatchTransOrderDTO = new AccountBatchTransOrderDTO();
        accountBatchTransOrderDTO.setBorrowerUserId(accountBatchTransOrderDTO.getBorrowerUserId());
        accountBatchTransOrderDTO.setProjectId(accountBatchTransOrderDTO.getProjectId());
        accountBatchTransOrderDTO.setProjectTitle(accountBatchTransOrderDTO.getProjectTitle());
        accountBatchTransOrderDTO.setAmount(accountBatchTransOrderDTO.getAmount());
        accountBatchTransOrderDTO.setCurrentPeriod(accountBatchTransOrderDTO.getCurrentPeriod());
        accountBatchTransOrderDTO.setTotalPeriod(accountBatchTransOrderDTO.getTotalPeriod());
        accountBatchTransOrderDTO.setRefundDate(accountBatchTransOrderDTO.getRefundDate());
        this.repaymentMessageSend(accountBatchTransOrderDTO,RepayMsgTypeEnum.CARD_WITHHOLD_FAIL);
    }

    public void sendRefundResult(Long projectId, Integer period, String batchRequestNo) {
        RepaymentResultDTO dto = new RepaymentResultDTO();
        dto.setProjectId(projectId);
        dto.setPeriod(period);
        dto.setBatchRequestNo(batchRequestNo);
        dto.setResult(false);
        rabbitTemplate.convertAndSend(MqConstants.Exchange.ASSET_EXCHANGE, MqConstants.getRoutingKey(MqConstants.Exchange.ASSET_EXCHANGE, MqConstants.Queue.REPAYMENT_RESULT), dto);

        log.info("还款失败发送消息通知资产端(projectId:{}, currentPeriod:{}, batchRequestNo:{})", projectId, period, batchRequestNo);
    }
}
